package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.SharePassageDao;

@WebServlet(name = "SharePassageServlet")
public class SharePassageServlet {
	private void printContent(PrintWriter out) {
    }
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws Exception,ServletException, IOException {
		 resp.setContentType("text/html;charset=\"utf-8\"");
	     PrintWriter out = resp.getWriter();
	     String method = req.getParameter("method");
	     
	     if (method.equals("share")) {
	    	 try {

	                //获取从分享界面传递过来的参数值

	                String shareTitle = new String(req.getParameter("share_title").getBytes("ISO-8859-1"),"UTF-8");
	                String shareContent=new String(req.getParameter("share_content").getBytes("ISO-8859-1"),"UTF-8");
	                String shareID = new String(req.getParameter("ID").getBytes("ISO-8859-1"),"UTF-8");
	                String shareCity = new String(req.getParameter("share_city").getBytes("ISO-8859-1"),"UTF-8");
	                int shareCost = new Integer(req.getParameter("share_cost"));
	                int shareDays = new Integer(req.getParameter("share_days"));
	                String shareTime = new String(req.getParameter("share_time").getBytes("ISO-8859-1"),"UTF-8");
	                SharePassageDao connect = new SharePassageDao();



	                if(shareID.equals(""))
	                {
	                    resp.getWriter().print("<script> alert('请先登录！');location.href=\"login.jsp\"; </script>");
	                }

	                int result = connect.share(shareID,shareTitle,shareContent,shareCity,shareCost,shareDays,shareTime);
	                if (result == 0) {
	                    SharePassageDao returnID = new SharePassageDao();
	                    int passageID = returnID.returnPassageID(shareTitle);
	                    resp.getWriter().print("<script> alert('分享成功！');location.href=\"travelDetails.jsp?passageID="+ passageID +"&page=1\";</script>");
	                }
	                if (result == 1) {
	                    resp.getWriter().print("<script> alert('error！'); location.href=\"index.jsp\";</script>");
	                }


	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            }
	     }
	}
	 
	 
}
